using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Xml;

using MySql.Data;
using MySql.Data.MySqlClient;

namespace Pitoshka.DB.MySql
{
	public class MySqlEngine : SqlEngine
	{
		MySqlConnection mySqlConnection;
		protected override DbConnection Connection
		{
			get { return mySqlConnection; }
		}

		public MySqlEngine() : this(PreferredConnString) { }
		public MySqlEngine(string connectionString) : base(connectionString)
        {
			this.mySqlConnection = new MySqlConnection(connectionString);
        }


		private int _LastInsertedId;
		public int LastInsertedId
		{
			get { return _LastInsertedId; }
		}
	


		public override DataTable ExecuteSelect(string sql, System.Data.CommandType commandType, DBParameter[] parameters)
		{
			OpenConnection();
			MySqlCommand mySqlCommand = this.CreateDbCommand(sql, commandType, parameters);

			MySqlDataAdapter dataAdapter = new MySqlDataAdapter(mySqlCommand);
			 
			DataTable dataTable = new DataTable();
			//dataTable.BeginLoadData();
			dataAdapter.Fill(dataTable);
			//dataTable.EndLoadData();
			dataAdapter.Dispose();
			mySqlCommand.Dispose();
			CloseConnection();
			return dataTable; 
		}

		public override DataSet ExecuteDataSet(string sql, CommandType commandType, DBParameter[] parameters)
		{
			throw new NotImplementedException();
		}

		public override XmlDocument ExecuteXML(string sql, System.Data.CommandType commandType, DBParameter[] parameters)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override XmlReader ExecuteXMLreader(string sql, System.Data.CommandType commandType, DBParameter[] parameters)
		{
			throw new Exception("The method or operation is not implemented.");
		}

		public override int ExecuteNonQuery(string sql, System.Data.CommandType commandType, DBParameter[] parameters)
		{
			OpenConnection();
			MySqlCommand sqlCommand = this.CreateDbCommand(sql, commandType, parameters);

			int retval = sqlCommand.ExecuteNonQuery();
			
			_LastInsertedId = Convert.ToInt32(sqlCommand.LastInsertedId);

			sqlCommand.Dispose();
			CloseConnection();
			return retval;
		}

		public override object ExecuteScalar(string sql, System.Data.CommandType commandType, DBParameter[] parameters)
		{
			OpenConnection();
			MySqlCommand sqlCommand = this.CreateDbCommand(sql, commandType, parameters);
			object retval = sqlCommand.ExecuteScalar();
			sqlCommand.Dispose();
			CloseConnection();
			return retval;
		}

		public override DbDataReader ExecuteReader(string sql, System.Data.CommandType commandType, DBParameter[] parameters)
		{
			OpenConnection();
			MySqlCommand sqlCommand = this.CreateDbCommand(sql, commandType, parameters);
			DbDataReader retval = sqlCommand.ExecuteReader();
			sqlCommand.Dispose();

			return retval;
		}

		public new MySqlCommand CreateDbCommand(string sql, CommandType commandType, DBParameter[] parameters)
		{
			MySqlCommand cmd = new MySqlCommand(sql, this.mySqlConnection);
			cmd.CommandType = commandType;

			if (parameters != null)
			{
				foreach (DBParameter parameter in parameters)
				{
					cmd.Parameters.Add(new MySqlParameter(parameter.Name, parameter.Value));
				}
			}

			return cmd;

		}
	}
}
